const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: {
            userName: '',
            userTel: '',
            userImg: ''
        },
        userPhoto: '',
        fileList: [],
        path: app.globalData.path,
        fileFlag: 0
    },

    onChooseAvatar(e) {
        var user = this.data.user, that = this;
        user.userImg = e.detail.avatarUrl
        this.setData({
            user
        })
        that.setData({
            userPhoto: e.detail.avatarUrl,
        })

        console.log(user);
    },
    // 获取手机号
    getPhoneNumber(e) {
        console.log(e);
    },

    // 上传头像
    upHandle() {
        var that = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            camera: 'back',
            success(res) {
                console.log(res.tempFiles)
                that.setData({
                    userPhoto: res.tempFiles[0],
                    fileList: res.tempFiles,
                })
                console.log(that.data.userPhoto);
            }
        })
    },



    // 保存
    saveHandle() {
        var user = this.data.user;
        // user.userImg == null &&
        if (user.userImg == null && this.data.userPhoto == '') {
            wx.showToast({
                title: '请上传头像',
                icon: 'none'
            })
            return
        } else if (user.userName == '') {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none'
            })
            return
        } else if (user.userTel == '') {
            wx.showToast({
                title: '请输入联系方式',
                icon: 'none'
            })
            return
        } else if (!this.validator(user.userTel)) {
            wx.showToast({
                title: '您的联系方式输入有误  ',
                icon: 'none'
            })
            return
        }

        console.log(user);

        var url = app.globalData.url + "user/editUser",
            that = this;
        wx.showLoading({
            title: '',
        })
        console.log(that.data.fileList);
        console.log(user);
        // return
        if (this.data.userPhoto != '') {
            wx.uploadFile({
                filePath: that.data.user.userImg,
                name: 'file',
                url: url,
                formData: {
                    userId: app.globalData.userId,
                    userName: user.userName,
                    userTel: user.userTel,
                },
                success(res) {
                    var res = JSON.parse(res.data)
                    console.log(res);
                    app.globalData.user = res.data
                    wx.setStorageSync('user', JSON.stringify(res.data))
                    if (res.flag) {
                        wx.hideLoading()
                        wx.showModal({
                            title: '提示',
                            content: '信息保存成功',
                            showCancel: false,
                            complete: (res) => {
                                if (res.confirm) {
                                    wx.navigateBack()
                                }
                            }
                        })
                    }
                }
            })
        } else {
            var url = app.globalData.url + 'user/editUsernameAndTf'
            app.wxRequest(url, {
                userId: app.globalData.userId,
                name: user.userName,
                userTel: user.userTel,
            }, res => {
                console.log(res);
                wx.hideLoading()
                if (res.flag) {
                    console.log(user);
                    var newUser = app.globalData.user
                    newUser.userName = user.userName
                    newUser.userTel = user.userTel
                    app.globalData.user = newUser
                    wx.setStorageSync('user', JSON.stringify(newUser))
                    console.log(app.globalData.user);
                    wx.showModal({
                        title: '提示',
                        content: '提交成功',
                        showCancel: false,
                        complete: (res) => {
                            if (res.confirm) {
                                wx.switchTab({
                                    url: '/pages/geren/geren',
                                })
                            }
                        }
                    })
                    console.log(that.data);
                } else {
                    wx.showToast({
                        title: '更新失败，请重试',
                        icon: 'none'
                    })
                }
            },
                err => {
                    console.log(err);
                })
        }
        console.log(user);
    },



    // 验证手机号 
    validator: function (val) {
        return /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/.test(val) || /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/.test(val);
    },

    // 输入框编辑
    inputHandle(e) {
        var str = e.currentTarget.dataset.flag
        var val = e.detail.value
        this.setData({
            [str]: val
        })
        console.log(this.data);
    },

    // 选择出生日期
    showTimeHandle() {
        this.setData({
            showTime: true
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var user = app.globalData.user || JSON.parse(wx.getStorageSync('user'));
        console.log(user);
        if (user.userName == null) {
            wx.showToast({
                title: '请完善您的个人信息',
                icon: 'none'
            })
            user.userName == ''
        }
        if (user.userTel == null) {
            user.userTel == ''
        }
        if (user.userImg == null) {
            user.userImg == ''
        }
        // user.userImg = null
        this.setData({
            user
        })
        console.log(this.data);

        console.log(app.globalData.user);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() { },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() { },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})